/**
 * Created by Eoly on 2017/2/16.
 * Call http://palerock.cn
 */
var groupDetailPage = angular.module('groupDetailPage', []);


groupDetailPage.filter('beToGroupStatus', function () {
    return function (status) {
        return app.beToGroupStatus(status)
    };
});

/**
 * 团详情
 */
groupDetailPage.controller('groupDetail', function ($scope, $http) {

    //声明参数
    $scope.paramGet = {};
    $scope.paramJoin = {};

    //声明获取的团对象
    $scope.group = {};

    //声明地址数组
    $scope.addresses = [];

    //获取在列表页储存的团id
    $scope.getTheGroupDetail = function (isRefresh) {
        $scope.paramGet.groupId = sessionStorage.getItem('ptsc_detail_groupId');

        if (!$scope.paramGet.groupId) {
            $.alert("未指定团信息", function () {
                app.backLastPosition();
            });
            return;
        }

        if (!isRefresh) {
            $.showPreloader('请稍候');
        }

        $http(
            {
                method: 'get',
                params: $scope.paramGet,
                url: net + "/group/find-group"
            }
        ).then(
            //获取成功
            function (res) {

                if (!isRefresh) {
                    $.hidePreloader();
                }

                if (!res.data.error) {

                    console.log('获取成功');
                    console.log(res);

                    //暂时储存获取的对象
                    var group = res.data.data;

                    //转化团封面图片为可识别的url数组
                    //声明一个imgSrc接收转化为数组的图片地址
                    group.imgSrc = eval(res.data.data.groupImgs);
                    for (var i = 0; i < group.imgSrc.length; i++)
                        //遍历图片地址并加上服务器的url
                        group.imgSrc[i] = net + '/resources/img/' + group.imgSrc[i];

                    //转化团规格为可识别的对象
                    group.format = eval('(' + res.data.data.groupFormat + ')');

                    //将group在$scope中绑定
                    $scope.group = group;

                    if (isRefresh) {
                        $.toast('刷新成功', 800, 'refresh-suc');
                        $.pullToRefreshDone('.pull-to-refresh-content');
                    }

                    setTimeout(function () {
                        $(function () {
                            $.initSwiper($('.content'));
                        });
                    }, 500);
                } else {
                    $.alert("错误代码:" + res.data.error + "," + res.data.mes, function () {
                        $.pullToRefreshDone('.pull-to-refresh-content');
                    })
                }

            },
            //获取失败
            function (res) {
                if (!isRefresh) {
                    $.hidePreloader();
                }
                $.alert('网络或设置错误');
            }
        )
    };

    //获取的用户地址列表
    $scope.addresses = [];

    //获取用户地址列表
    $scope.getAddresses = function () {
        var token = app.getUserToken();

        if (!token) {
            return;
        }

        if (token) {
            $http({
                method: 'get',
                url: net + '/user/show-address',
                headers: {'Authorization': 'Bearer ' + token}
            }).then(function (res) {
                console.log(res);
                if (!res.data.error) {
                    $scope.addresses = res.data.dataList;
                    $scope.addresses.unshift({addressInfo:'点击选择收货地址'});
                } else {
                    $.alert("错误代码:" + res.data.error + "," + res.data.mes)
                }
            }, function () {
                $.alert('网络或设置错误');
            })
        }
    };

    //参加团
    $scope.joinGroup = function(){

        sessionStorage.removeItem('ptsc_none_login');

        var token = app.getUserToken();

        if (!token) {
            return;
        }

        if($scope.paramGet.groupId==null) {
            return;
        }

        $scope.paramJoin.groupId = $scope.group.groupId;

        $http(
            {
                method: 'post',
                params: $scope.paramJoin,
                url: net + "/user/group/join-group",
                headers : {'Authorization' : 'Bearer ' + token}
            }
        ).then(
            function (res) {

                if(!res.data.error) {

                    $.alert(res.data.mes,function () {
                        var preOrder = JSON.stringify(res.data.data);
                        sessionStorage.setItem('ptsc_pre_order',preOrder);
                        app.turnHref('group-detail.html','#','user-order.html#pay');
                    });


                }else{
                    if(res.data.error == 731){
                        if($scope.paramJoin.addressId == null){
                            $.toast('你还没有选择收货地址',800,'toastToBot');
                            return;
                        }
                    }
                    $.alert("错误代码:"+res.data.error+","+res.data.mes)
                }

            },
            //获取失败
            function (res) {
                $.alert('网络或设置错误');
            },
            //获取中
            function (res) {
                console.log(res);
            }
        )
    };
});